const Koa = require('koa');
const app = new Koa();
const cors = require('koa2-cors')
const bodyParser = require('koa-bodyparser')

const staticCache = require('koa-static-cache')
const { corsHandler } = require('./middle/cors')
const { loggerMiddleware } = require('./middle/logger')
const config = require('./config/config')

// 设置路由路径
const index = require('./routers/index')
const mysql = require('./routers/mysql')


// 挂载日志
app.use(loggerMiddleware)

// 处理post参数
app.use(bodyParser())

// 缓存客户端的服务器响应
app.use(staticCache(config.publicDir, {
    maxAge: 365 * 24 * 60 * 60  //Add these files to caches for a year
}))

// cors[请求头和响应头特殊配置]
app.use(cors(corsHandler))


// app.use(async (ctx, next) => {
//     console.log(ctx.req)
// })

// 配置路由
app
    .use(index.routes(), index.allowedMethods())
    .use(mysql.routes(), mysql.allowedMethods())

module.exports = app
